const productCategory = document.getElementById("product-category");
const productDetailNav = document.getElementById("nav-span");
let urlParams = new URLSearchParams(window.location.search);
let category = urlParams.get("category");
let position = urlParams.get("position");
let productName = urlParams.get("name");
if (category && position) {
  category = decodeURIComponent(category);
  console.log(category);
  productCategory.innerText = category;
  position = decodeURIComponent(position);
  position = position.replace(/"/g, "");
  console.log(position);
  productCategory.href = `./products?position=${position}`;
  productName = decodeURIComponent(productName);
  console.log(productName);
  productDetailNav.innerText = `> ${productName}`;
}

const qrModal = document.getElementById("qrModal");

function showQRModal() {
  qrModal.style.display = "flex";
  document.body.style.overflow = "hidden";  
}

function hideQRModal() {
  qrModal.style.display = "none";
  document.body.style.overflow = "auto";
}

// 点击遮罩层关闭
qrModal.addEventListener("click", function (e) {
  if (e.target === qrModal) {
    hideQRModal();
  }
});

// 阻止二维码区域的点击事件冒泡
document.querySelector(".mobile-qr-code-box").addEventListener("click", function (e) {
  e.stopPropagation();
});
document
    .querySelector(".mobile-qr-code-box")
    .addEventListener("click", function () {
      showQRModal();
      console.log('点击展示二维码');
    });

// 检测是否为移动设备
function isMobile() {
  return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
    navigator.userAgent
  );
}

if (isMobile()) {
  document.querySelector(".qr-code-box").style.display = "none";
  document.querySelector(".mobile-qr-code-box").style.display = "flex";
}
